package org.ephone;

import android.app.Activity;
import android.os.Looper;
import android.view.KeyEvent;

public class KeyGenerator extends Thread {
	//String[] command = new String[1]; //XXX
	ePhoneApp appCommand;
	KeyEvent backEvtDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN); 
    KeyEvent backEvtUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN);
    private boolean intoActivity = false;
    
    public int keyEvent;
    
    private Activity activityIstance;
    private int typeOfActivity; // 0 for Contacts ; 1 for DeepContacts
    
    public KeyGenerator(Activity istance, int type){
    	activityIstance = istance;
    	typeOfActivity = type;
    	appCommand = (ePhoneApp)activityIstance.getApplicationContext();
    }
    
    public void exitActivity(){
    	intoActivity = false;
    }
    
    private void generateKeys(int keyCode){
    	switch(typeOfActivity){
			case 0: // ContactsActivity
				ContactsActivity activity = (ContactsActivity)activityIstance;
				backEvtDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); 
	            backEvtUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode); 
	            activity.onKeyDown(keyCode, backEvtDown); 
	            activity.onKeyUp(keyCode, backEvtUp);
	            activity.onContentChanged();
				break;
			case 1: // DeepContactsActivity
				DeepContactsActivity activityD = (DeepContactsActivity)activityIstance;
				backEvtDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); 
	            backEvtUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode); 
	            activityD.onKeyDown(keyCode, backEvtDown); 
	            activityD.onKeyUp(keyCode, backEvtUp);
	            activityD.onContentChanged();
				break;
		}
    }
    
	@Override
	public void run() { 
		Looper.prepare();  
		intoActivity = true;
		int count = 0;
		
		while (intoActivity){
			try {
				
				String resComm = appCommand.getCommand();

				int commandInt = Integer.parseInt(resComm);

				switch (commandInt){
					case 0x10:
						keyEvent = KeyEvent.KEYCODE_DPAD_UP;
						break;
					case 0x20:
						keyEvent = KeyEvent.KEYCODE_DPAD_DOWN;
						break;
					case 0x40:
						keyEvent = KeyEvent.KEYCODE_DPAD_LEFT;
						break;
					case 0x80:
						keyEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
						break;
					case 0x01:
						keyEvent = KeyEvent.KEYCODE_DPAD_CENTER;
						break;
					case 0x02:
						keyEvent = KeyEvent.KEYCODE_MENU;
						break;

					/* Test
					case 00:
						if (count % 2 == 0)
							keyEvent = KeyEvent.KEYCODE_DPAD_DOWN;
						else
							keyEvent = KeyEvent.KEYCODE_DPAD_UP;
						break;
					*/
				}
				// ContactsActivity
				if (typeOfActivity == 0){
					//((ContactsActivity)activityIstance).test.setText((new Integer(count)).toString() + " " + command[0]);
					((ContactsActivity)activityIstance).handler.post( new Runnable() {
	                    public void run() {
	                    	//((ContactsActivity)activityIstance).test.show();
	                    	generateKeys(keyEvent);
	                    }
	                });
				} else if (typeOfActivity == 1){ // DeepContactsActivity
					//((DeepContactsActivity)activityIstance).test.setText((new Integer(count)).toString() + " " + command[0]);
					((DeepContactsActivity)activityIstance).handler.post( new Runnable() {
	                    public void run() {
	                    	//((DeepContactsActivity)activityIstance).test.show();
	                    	generateKeys(keyEvent);
	                    }
	                });
					
				}
				
				
				
			} catch (Exception ee){
				if (typeOfActivity == 0){
					((ContactsActivity)activityIstance).test.setText(ee.toString());
					((ContactsActivity)activityIstance).test.show();
				} else if (typeOfActivity == 1){
					((DeepContactsActivity)activityIstance).test.setText(ee.toString());
					((DeepContactsActivity)activityIstance).test.show();
				}
			}
			count++;
		
		}
        //Looper.loop();
    }
}
